﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;

namespace XMLMovieWebsite
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        Login objUser = new Login("","");
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                objUser = Session["sesLogin"] as Login;
            }
            catch { }
            if (objUser != null)
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                Label2.Text += objUser.UserName;
            }
            else
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }

            loadDulieuRaFileXML();
            layDulieuTuXML();
        }

        //protected void btDangNhap_Click(object sender, EventArgs e)
        //{
        //    objUser = new Login(TextBox1.Text.Trim(), TextBox2.Text.Trim());
        //    if (objUser.IsLoged)
        //    {
        //        Session["sesLogin"] = objUser;
        //        Response.Redirect("Default.aspx"); //Nếu đúng tên đăng nhập và mật khẩu thì gọi trang Default.aspx hiển thị
        //    }
        //    else
        //    {
        //        Label1.Text = objUser.Message; //Thông báo lỗi ra màn hình
        //    }
        //}


        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("Register.aspx"); 
        //}

        public void loadDulieuRaFileXML()
        {
            try
            {
                string ConString = ConfigurationManager.ConnectionStrings["ConStringXml"].ConnectionString;
                //string CMDString = "SELECT id,name,time, rating" + "FROM allMovie FOR XML RAW ('Movie'), ROOT('root'), ELEMENTS";
                string CMDString = " SELECT top 5 *  FROM movie_thao ORDER BY  LuotXem DESC ";
                SqlConnection con;
                SqlCommand cmd;
                SqlDataAdapter sda;
                DataTable dt;


                using (con = new SqlConnection(ConString))
                {
                    cmd = new SqlCommand(CMDString, con);
                    con.Open();
                    dt = new DataTable("Movie");
                    sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
                    if (dt != null)
                    {


                        dt.WriteXml(Server.MapPath("~/App_Data/Top5.xml"));
                    }
                    con.Close();

                }
            }
            catch (Exception)
            {

            }


        }
        public void layDulieuTuXML()
        {

            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(Server.MapPath("~/App_Data/Top5.xml"));
                //GridView1.DataSource = ds;
                //GridView1.DataBind();
                DataTable dt = new DataTable();
                dt = ds.Tables["Movie"];
                //Image1.ImageUrl =  dt.Rows[0][11].ToString();

                Test.DataSource = dt;
                Test.DataBind();

            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("SreachMovie.aspx?Ten=" + TextBox3.Text);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
                Session.RemoveAll();
                Response.Redirect("Default.aspx"); 
        }

    }
}
